var http = require("../../utils/http.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    settledAmount:0,
    amount:null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getDisInfoData();
    this.loadWithdrawCashRule()
  },

  /**
 * 获取用户钱包数据
 */
  getDisInfoData: function () {
    wx.showLoading();
    var params = {
      url: "/p/distribution/user/info",
      method: "GET",
      data: {
        shopId: 1
      },
      callBack: res => {
        this.setData({
          settledAmount: res.distributionUserWallet.settledAmount,
          addupAmount: res.distributionUserWallet.addupAmount
        });
        wx.hideLoading();
      }
    }
    http.request(params);
  },

  /**
   * 确认提现
   */
  confirmWithdraw:function(){
    if (!this.data.amount || this.data.amount<=0){
      return;
    } else if (this.data.amount>this.data.settledAmount){
      wx.showToast({
        title: '余额不足',
        icon:"none"
      })
      return;
    }

    wx.showLoading({
      mask:true
    })
    http.request({
      url: "/p/distribution/withdrawCash/save",
      method: "POST",
      data: {
        shopId: 1,
        amount: this.data.amount
      },
      callBack: res => {
       wx.hideLoading();
       wx.showModal({
         content: '提现申请已提交',
         showCancel: false,
         complete: function () {
           wx.navigateBack();
         }
       })
       
      }
    });
  },

  onAmountInput:function(e){
    this.setData({
      amount:e.detail.value
    });
  },

  onAllWithdraw:function(){
    this.setData({
      amount: this.data.settledAmount
    });
  },


  /**
   * 加载提现规则
   */
  loadWithdrawCashRule: function () {
    var ths = this;
    wx.showLoading();
    var params = {
      url: "/p/distribution/withdrawCash/info",
      method: "GET",
      data: {
        shopId: 1
      },
      callBack: function (res) {
        wx.hideLoading();
        ths.setData({
          frequency: res.frequency,
          number: res.number,
          amountMax: res.amountMax,
          amountMin: res.amountMin,
          paymentExplain: res.paymentExplain
        });
      }
    };
    http.request(params);
  }


})
